package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.*;
import com.ruoyi.background.mapper.AppGradeRecordMapper;
import com.ruoyi.background.mapper.AppSiteTopicUserMapper;
import com.ruoyi.background.service.IAppSiteService;
import com.ruoyi.background.service.IAppSiteTopicUserDetailService;
import com.ruoyi.background.service.IAppSiteTopicUserService;
import com.ruoyi.background.service.IAppWechatUserService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户站点答题主记录Service业务层处理
 *
 * @author susu
 * @date 2024-08-27
 */
@Service
public class AppSiteTopicUserServiceImpl implements IAppSiteTopicUserService {
    @Autowired
    private AppSiteTopicUserMapper appSiteTopicUserMapper;

    @Autowired
    private IAppSiteService siteService;

    @Autowired
    private IAppWechatUserService wechatUserService;

    @Autowired
    private AppGradeRecordMapper appGradeRecordMapper;

    @Autowired
    private IAppSiteTopicUserDetailService detailService;

    /**
     * 查询用户站点答题主记录
     *
     * @param id 用户站点答题主记录主键
     * @return 用户站点答题主记录
     */
    @Override
    public AppSiteTopicUser selectAppSiteTopicUserById(Long id) {
        return appSiteTopicUserMapper.selectAppSiteTopicUserById(id);
    }

    /**
     * 查询用户站点答题主记录列表
     *
     * @param appSiteTopicUser 用户站点答题主记录
     * @return 用户站点答题主记录
     */
    @Override
    public List<AppSiteTopicUser> selectAppSiteTopicUserList(AppSiteTopicUser appSiteTopicUser) {
        return appSiteTopicUserMapper.selectAppSiteTopicUserList(appSiteTopicUser);
    }

    /**
     * 新增用户站点答题主记录
     *
     * @param appSiteTopicUser 用户站点答题主记录
     * @return 结果
     */
    @Override
    public int insertAppSiteTopicUser(AppSiteTopicUser appSiteTopicUser) {
        appSiteTopicUser.setCreateTime(DateUtils.getNowDate());
        return appSiteTopicUserMapper.insertAppSiteTopicUser(appSiteTopicUser);
    }

    /**
     * 修改用户站点答题主记录
     *
     * @param appSiteTopicUser 用户站点答题主记录
     * @return 结果
     */
    @Override
    public int updateAppSiteTopicUser(AppSiteTopicUser appSiteTopicUser) {
        return appSiteTopicUserMapper.updateAppSiteTopicUser(appSiteTopicUser);
    }

    /**
     * 批量删除用户站点答题主记录
     *
     * @param ids 需要删除的用户站点答题主记录主键
     * @return 结果
     */
    @Override
    public int deleteAppSiteTopicUserByIds(Long[] ids) {
        return appSiteTopicUserMapper.deleteAppSiteTopicUserByIds(ids);
    }

    /**
     * 删除用户站点答题主记录信息
     *
     * @param id 用户站点答题主记录主键
     * @return 结果
     */
    @Override
    public int deleteAppSiteTopicUserById(Long id) {
        return appSiteTopicUserMapper.deleteAppSiteTopicUserById(id);
    }


    @Async
    @Transactional
    @Override
    public void setUserTopicData(List<AppSiteTopic> topicList, AppSiteTopicUser topicUser) {
        //插入用户的答题记录
        int i = this.insertAppSiteTopicUser(topicUser);
        Long id = topicUser.getId();
        Date nowDate = DateUtils.getNowDate();
        List<AppSiteTopicUserDetail> detailList = new ArrayList<>();
        for (AppSiteTopic appSiteTopic : topicList) {
            AppSiteTopicUserDetail detail = new AppSiteTopicUserDetail();
            detail.setRecordId(id);
            detail.setTitle(appSiteTopic.getTitle());
            detail.setAnswer(appSiteTopic.getTanser());
            detail.setUserAnswer(appSiteTopic.getUserAnsWerString());
            detail.setIsRight(appSiteTopic.getIsTrue());
            detail.setChoose1(appSiteTopic.getChoose1());
            detail.setChoose2(appSiteTopic.getChoose2());
            detail.setChoose3(appSiteTopic.getChoose3());
            detail.setChoose4(appSiteTopic.getChoose4());
            detail.setTopicType(Math.toIntExact(appSiteTopic.getTopicType()));
            detail.setParse(appSiteTopic.getParse());
            detail.setCreateTime(nowDate);
            detailList.add(detail);
        }
        detailService.insertAppSiteTopicUserDetailList(detailList);
        //如果全队 奖励积分
        if (Objects.equals(topicUser.getTotalSum(), topicUser.getTrueSum())) {
            System.out.println("用户全对");
            AppSiteTopicUser query = new AppSiteTopicUser();
            query.setSiteId(topicUser.getSiteId());
            query.setUserId(topicUser.getUserId());
            query.setIsAllRight(0);
            List<AppSiteTopicUser> list = appSiteTopicUserMapper.selectAppSiteTopicUserList(query);
            if (list == null || list.size() <= 1) {
                //第一次全对 奖励积分
                AppSite site = siteService.selectAppSiteById(topicUser.getSiteId());
                Integer rewardGrade = site.getRewardGrade();//奖励的积分
                //插入积分消耗的记录
                AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId(topicUser.getUserId());
                AppGradeRecord appGradeRecord = new AppGradeRecord();
                appGradeRecord.setUserId(wechatUser.getUserId());
                appGradeRecord.setType(0);
                appGradeRecord.setGrade(rewardGrade);
                appGradeRecord.setBeforeGrade(wechatUser.getGrade());
                appGradeRecord.setRemark(site.getName() + "答题全对奖励！");
                appGradeRecord.setCreateTime(DateUtils.getNowDate());
                appGradeRecordMapper.insertAppGradeRecord(appGradeRecord);
                //修改用户的积分
                AppWechatUser updateUser = new AppWechatUser();
                updateUser.setId(wechatUser.getId());
                updateUser.setGrade(wechatUser.getGrade() + rewardGrade);
                wechatUserService.updateAppWechatUser(updateUser);
            }
        }


    }
}
